package com.adu.springbootdemo.controller;

import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class BaseController {
    // 页数
    public static final String PAGE_NUM="pageNum";
    // 每页数据量
    public static final String PAGE_SIZE="pageSize";
    // 排序列
    public static final String ORDER_BY_COLUMN="orderByColumn";
    // 排序方向：desc||asc
    public static final String IS_ASC="isAsc";

    /**
     * 设置请求分页数据
     */
    public void startPage(){
        ServletRequestAttributes attributes =(ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request=attributes.getRequest();
        String pageNumStr=request.getParameter(PAGE_NUM);
        String pageSizeStr=request.getParameter(PAGE_SIZE);
        if(StringUtils.isNotEmpty(pageNumStr) && StringUtils.isNotEmpty(pageSizeStr)){
            int pageNum=Integer.parseInt(request.getParameter(PAGE_NUM));
            int pageSize=Integer.parseInt(request.getParameter(PAGE_SIZE));
            PageHelper.startPage(pageNum,pageSize);
        }

    }
}
